package me.youline.dataServer.service.socialAction.handler;

import java.util.List;

import me.youline.dataServer.dao.socialActionMsg.SocialActionPraiseMsgDAO;
import me.youline.dataServer.entity.socialActionMsg.SocialActionBaseMsg;
import me.youline.dataServer.entity.socialActionMsg.SocialActionPraiseMsg;
import me.youline.dataServer.service.SocialActionNotifyMsgService;
import me.youline.dataServer.service.socialAction.event.PraiseSocialActionCancelEvent;
import me.youline.dataServer.service.socialAction.event.PubMsgSocialActionExeEvent.PraiseSocialActionExeEvent;
import me.youline.dataServer.service.socialAction.event.SocialActionCancelEvent;
import me.youline.dataServer.service.socialAction.event.SocialActionEvent;
import me.youline.dataServer.service.socialAction.event.SocialActionExeEvent;
import me.youline.dataServer.service.support.MobilePushService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;

/**
 * 点赞社交行为事件处理器，主要负责点赞执行事件处理，及取消点赞事件处理。
 * 该类中定义了，点赞动态信息数据的构成、点赞动态的通知格式、持久化点赞动态、取消事件下执行的动态数据清除业务。
 * @author linhan
 *
 */
@Component
public class PraiseSocialActionEventHandler extends AbstractSocialActionEventHandler {
	
	@Autowired
	private MobilePushService mobilePushService;
	
	@Autowired
	private SocialActionNotifyMsgService socialActionNotifyMsgService;
	
	@Autowired
	private SocialActionPraiseMsgDAO praiseMsgDAO;
	
	private static final String MSG_BODY = "@%s 点赞了您的一条内容，快去看看吧!";
	

	@Override
	public boolean isSupport(SocialActionEvent event) {
		if( event instanceof PraiseSocialActionExeEvent){
			return true;
		}
		if(event instanceof PraiseSocialActionCancelEvent){
			return true;
		}
		return false;
	}

	@Override
	List<Long> decideNotifiedUsers(SocialActionExeEvent event) {
		PraiseSocialActionExeEvent e = (PraiseSocialActionExeEvent) event;
		return Lists.newArrayList(e.getTargetObject().getCreatorId());
	}

	@Override
	protected SocialActionBaseMsg extractMsgExtensionProp(
			SocialActionExeEvent event) {
		SocialActionPraiseMsg msg = new SocialActionPraiseMsg();
		PraiseSocialActionExeEvent e = (PraiseSocialActionExeEvent) event;
		msg.setMsgPicUrl(e.getTargetObject().getMsgPicUrl());
		msg.setNotifiedUserId(e.getTargetObject().getCreatorId());
		return msg;
	}

	@Override
	protected void handlerCancelEvent(SocialActionCancelEvent event) {
		socialActionNotifyMsgService.deletePraiseNotifyMsg(event.getInitiator(), event.getTargetId());
	}


	@Override
	protected void doMsgAndEventProcess(SocialActionBaseMsg msg,
			SocialActionExeEvent event) {
		Long id = praiseMsgDAO.addSocialActionNotifyMsg((SocialActionPraiseMsg) msg);
		msg.setId(id);
	}

	@Override
	protected String getPushFormat() {
		return MSG_BODY;
	}

}
